home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / tablet / scribble.cpp.z / scribble.cpp
Encoding:
C/C++ Source or Header  |  2002-04-08  |  3.7 KB  |  136 lines

  1. /****************************************************************************
  2. ** $Id:  qt/scribble.cpp   3.0.3   edited Oct 12 12:18 $
  3. **
  4. ** Copyright ( C ) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "canvas.h"
  12. #include "scribble.h"
  13.  
  14. #include <qapplication.h>
  15. #include <qevent.h>
  16. #include <qpainter.h>
  17. #include <qtoolbar.h>
  18. #include <qtoolbutton.h>
  19. #include <qspinbox.h>
  20. #include <qtooltip.h>
  21. #include <qrect.h>
  22. #include <qpoint.h>
  23. #include <qcolordialog.h>
  24. #include <qfiledialog.h>
  25. #include <qcursor.h>
  26. #include <qimage.h>
  27. #include <qstrlist.h>
  28. #include <qpopupmenu.h>
  29. #include <qintdict.h>
  30.  
  31.  
  32.  
  33. Scribble::Scribble( QWidget *parent, const char *name )
  34.     : QMainWindow( parent, name )
  35. {
  36.     canvas = new Canvas( this );
  37.     setCentralWidget( canvas );
  38.  
  39.     QToolBar *tools = new QToolBar( this );
  40.  
  41.     bSave = new QToolButton( QPixmap(), "Save", "Save as PNG image", this, SLOT( slotSave() ), tools );
  42.     bSave->setText( "Save as..." );
  43.  
  44.     tools->addSeparator();
  45.  
  46.     bPColor = new QToolButton( QPixmap(), "Choose Pen Color", "Choose Pen Color", this, SLOT( slotColor() ), tools );
  47.     bPColor->setText( "Choose Pen Color..." );
  48.  
  49.     tools->addSeparator();
  50.  
  51.     bPWidth = new QSpinBox( 1, 20, 1, tools );
  52.     QToolTip::add( bPWidth, "Choose Pen Width" );
  53.     connect( bPWidth, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidth( int ) ) );
  54.     bPWidth->setValue( 3 );
  55.  
  56.     tools->addSeparator();
  57.  
  58.     bClear = new QToolButton( QPixmap(), "Clear Screen", "Clear Screen", this, SLOT( slotClear() ), tools );
  59.     bClear->setText( "Clear Screen" );
  60. }
  61.  
  62. /*
  63.  
  64. Scribble::Scribble( QWidget *parent, const char *name )
  65.     : QMainWindow( parent, name )
  66. {
  67.     canvas = new Canvas( this );
  68.     setCentralWidget( canvas );
  69.  
  70.     QToolBar *tools = new QToolBar( this );
  71.  
  72.     bSave = new QPushButton( "Save as...", tools );
  73.  
  74.     tools->addSeparator();
  75.  
  76.     bPColor = new QPushButton( "Choose Pen Color...", tools );
  77.     //    bPColor->setText( "Choose Pen Color..." );
  78.  
  79.     tools->addSeparator();
  80.  
  81.     bPWidth = new QSpinBox( 1, 20, 1, tools );
  82.     QToolTip::add( bPWidth, "Choose Pen Width" );
  83.     connect( bPWidth, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidth( int ) ) );
  84.     bPWidth->setValue( 3 );
  85.  
  86.     tools->addSeparator();
  87.  
  88.     bClear = new QPushButton( "Clear Screen", tools );
  89.     QObject::connect( bSave, SIGNAL( clicked() ), this, SLOT( slotSave() ) );
  90.     QObject::connect( bPColor, SIGNAL( clicked() ), this, SLOT( slotColor() ) );
  91.     QObject::connect( bClear, SIGNAL( clicked() ), this, SLOT( slotClear() ) );
  92.         
  93. }
  94.  
  95.   */
  96. void Scribble::slotSave()
  97. {
  98.     QPopupMenu *menu = new QPopupMenu( 0 );
  99.     QIntDict<QString> formats;
  100.     formats.setAutoDelete( TRUE );
  101.  
  102.     for ( unsigned int i = 0; i < QImageIO::outputFormats().count(); i++ ) {
  103.     QString str = QString( QImageIO::outputFormats().at( i ) );
  104.     formats.insert( menu->insertItem( QString( "%1..." ).arg( str ) ), new QString( str ) );
  105.     }
  106.  
  107.     menu->setMouseTracking( TRUE );
  108.     int id = menu->exec( bSave->mapToGlobal( QPoint( 0, bSave->height() + 1 ) ) );
  109.  
  110.     if ( id != -1 ) {
  111.     QString format = *formats[ id ];
  112.  
  113.     QString filename = QFileDialog::getSaveFileName( QString::null, QString( "*.%1" ).arg( format.lower() ), this );
  114.     if ( !filename.isEmpty() )
  115.         canvas->save( filename, format );
  116.     }
  117.  
  118.     delete menu;
  119. }
  120.  
  121. void Scribble::slotColor()
  122. {
  123.     QColor c = QColorDialog::getColor( canvas->penColor(), this );
  124.     canvas->setPenColor( c );
  125. }
  126.  
  127. void Scribble::slotWidth( int w )
  128. {
  129.     canvas->setPenWidth( w );
  130. }
  131.  
  132. void Scribble::slotClear()
  133. {
  134.     canvas->clearScreen();
  135. }
  136.